import {
  getProjectLists,
  getProjectCount,
  getConsUnit
} from '../../api/project'
import { _makeMediaSrc } from '@/utils/base'
import { resetObj } from '../../utils/resetObj'

const state = {
  date: '',
  projectSearchDTO: {
    // 页码
    index: 1,
    // 项目名称查询
    projectName: '',
    // 选中的施工单位
    consUnit: '',
    // 选中的地区(省级区划)
    province: '',
    // 选中的地区（市级区划）
    area: '',
    // 项目状态
    projectCompletion: '',
    // 项目编号
    projectId: '',
    // 单页数量
    pageSize: 20,
    // 用户id
    userId: ''
  },
  // 项目列表
  projectList: [],
  // 项目总数
  total: +'',
  // 全部和我的列表
  titleList: [
    { name: '全部', number: '' },
    { name: '我的', number: '' }
  ],
  currentNav: '全部',
  // 是否显示删除框
  deleteItem: {}

}
const getters = {
  // 项目开始时间
  createTime (state) {
    if (state.date === ('' || null)) {
      return ''
    } else {
      return state.date[0]
    }
  },
  // 结束时间
  endTime (state) {
    if (state.date === ('' || null)) {
      return ''
    } else {
      return state.date[1]
    }
  },
  // 最终上传的DTO
  searchDTO (state, getters) {
    const DTO = state.projectSearchDTO
    DTO.createTime = getters.createTime
    DTO.endTime = getters.endTime
    return DTO
  }
}
const mutations = {
  // 设置项目列表
  set_projectList (state, list) {
    state.projectList = list
  },

  // 修改删除对象
  set_deleteItem (state, obj) {
    state.deleteItem = obj
  },
  // 设置省级区划
  set_projectSearchDTO_province (state, str) {
    state.projectSearchDTO.province = str
  },
  // 设置项目总数
  set_total (state, total) {
    state.total = total
  },
  // 设置跳转页
  set_projectsearchDTO_index (state, index) {
    state.projectSearchDTO.index = index
  },
  set_projectsearchDTO_pageSize (state, pageSize) {
    state.projectSearchDTO.pageSize = pageSize
  },
  set_projectId (state, projectId) {
    state.projectSearchDTO.projectId = projectId
  }
}
const actions = {
  // 获取施工单位列表
  getConsUnit () {
    return new Promise((resolve, reject) => {
      getConsUnit()
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  // 条件获取项目列表
  getList ({ getters, commit }, payload) {
    return new Promise((resolve, reject) => {
      // console.log(getters.searchDTO)
      if (!payload) {
        payload = getters.searchDTO
      }
      getProjectLists(payload)
        .then(res => {
          if (res.data.code === 200) {
            if (!res.data.data.data.length) {
              commit('set_projectList', res.data.data.data)
              commit('set_total', res.data.data.count)
              return
            }
            const resData = res.data.data.data
            resData.forEach(async item => {
              item.ifSelect = false
              item.figure = await _makeMediaSrc(item.figure)
              commit('set_projectList', resData)
            })
            commit('set_total', res.data.data.count)
          }
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  // 重置查询条件
  async reset ({ state, dispatch }) {
    state.date = ''
    await resetObj(state.projectSearchDTO)
    state.projectSearchDTO.index = 1
    state.projectSearchDTO.pageSize = 20
    dispatch('getList')
  },
  // 获取项目数量
  getCount ({ state, getters }) {
    return new Promise((resolve, reject) => {
      getProjectCount(getters.searchDTO)
        .then(res => {
          if (res.data.data) {
            state.titleList[0].number = res.data.data.allCount
            state.titleList[1].number = res.data.data.myCount
          }
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  }
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
